package com.farmer.machinery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.common.entity.Machinery;
import com.farmer.common.result.Result;
import com.farmer.machinery.mapper.MachineryMapper;
import com.farmer.machinery.service.MachineryService;
import org.springframework.stereotype.Service;

@Service
public class MachineryServiceImpl extends ServiceImpl<MachineryMapper, Machinery> implements MachineryService {

    @Override
    public Result<?> list(Integer pageNum, Integer pageSize, String name, String type, String status) {
        Page<Machinery> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Machinery> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isBlank()) {
            queryWrapper.like("name", name);
        }
        if (type != null && !type.isBlank()) {
            queryWrapper.eq("type", type);
        }
        if (status != null && !status.isBlank()) {
            queryWrapper.eq("status", status);
        }

        baseMapper.selectPage(page, queryWrapper);

        return Result.success(page);
    }

    @Override
    public Result<?> add(Machinery machinery) {
        baseMapper.insert(machinery);
        return Result.success();
    }

    @Override
    public Result<?> update(Machinery machinery) {
        baseMapper.updateById(machinery);
        return Result.success();
    }

    @Override
    public Result<?> delete(Long id) {
        baseMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result<?> detail(Long id) {
        Machinery machinery = baseMapper.selectById(id);
        return Result.success(machinery);
    }
}